/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * MpmDataBoardCustomizeRecordController 数据看板自定义记录表
 * <p>
 * 修改纪录 2020-12-29 版本：1.0 wudaliang 创建。
 *
 * @version 版本：1.0
 * @author 作者：wudaliang 创建日期：2020-12-29
 */

package com.topscomm.mpm.controller;

import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.pojo.MpmDataBoardCustomizeRecordEntity;
import com.topscomm.mpm.service.impl.MpmDataBoardCustomizeRecordService;
import com.topscomm.pub.util.ConvertUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mpm.controller.auto.MpmDataBoardCustomizeRecordControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wudaliang
 * @date: 2020-12-29
 * @description:数据看板自定义记录表Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/dataBoardCustomizeRecord")
public class MpmDataBoardCustomizeRecordController extends
    MpmDataBoardCustomizeRecordControllerAuto implements IMpmDataBoardCustomizeRecordController {



    @Autowired
    private MpmDataBoardCustomizeRecordService mpmDataBoardCustomizeRecordService;

    /**
     * @author: wudaliang
     * @date: 2020-12-29
     * @param paraMap
     * @return
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_str")) {
            String value = paraMap.get("qry_str").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%')", value);
            }
        }
        if (paraMap.containsKey("qry_recordlevel")) {
            String value = paraMap.get("qry_recordlevel").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and recordlevel = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_position")) {
            String value = paraMap.get("qry_position").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and position = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_role")) {
            String value = paraMap.get("qry_role").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and  EXISTS(select code from CBOROLE where CBOROLE.CODE=MPMDATABOARDCUSTOMIZERECORD.ROLECODE and CBOROLE.NAME like '%{0}%')", value);
            }
        }
        if (paraMap.containsKey("qry_orgname")) {
            String value = paraMap.get("qry_orgname").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and EXISTS ( SELECT CODE  FROM CBOORG WHERE CBOORG.CODE = MPMDATABOARDCUSTOMIZERECORD.ORGCODE AND CBOORG.NAME LIKE '%{0}%' ) ", value);
            }
        }
        return sql.toString();
    }

    /**
     * @author: wudaliang
     * @date: 2020-12-29
     * @param paraMap
     * @return
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        return super.refList(paraMap);
    }


    @Override
    @PostMapping(value = "/insert")
    public ResponseResult<Object> insert(
        @RequestBody(required = false) Map<String, Object> paraMap) {

        List<BasicEntity> entityList = new ArrayList<BasicEntity>();
        if (paraMap.containsKey("orgcode")) {
            MpmDataBoardCustomizeRecordEntity entity = new MpmDataBoardCustomizeRecordEntity();
            entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
            entity.setOrgcode(ConvertUtil.convertToString(paraMap.get("orgcode")));
            entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
            entity.setRecordlevel("2");
            entity.setPriority("999");
            if(paraMap.containsKey("description")){
                entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
            }
            entityList.add(entity);
            mpmDataBoardCustomizeRecordService.insertBatch(entityList);
        } else if (paraMap.containsKey("rolecode")) {
            MpmDataBoardCustomizeRecordEntity entity = new MpmDataBoardCustomizeRecordEntity();
            entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
            entity.setRolecode(ConvertUtil.convertToString(paraMap.get("rolecode")));
            entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
            entity.setRecordlevel("1");
            entity.setPriority(ConvertUtil.convertToString(paraMap.get("priority")));
            if(paraMap.containsKey("description")){
                entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
            }
            entityList.add(entity);
            mpmDataBoardCustomizeRecordService.insertBatch(entityList);
        } else {
            List<Map<String, Object>> userList = (List<Map<String, Object>>) paraMap.get("userList");
            for (Map<String, Object> user : userList) {
                MpmDataBoardCustomizeRecordEntity entity = new MpmDataBoardCustomizeRecordEntity();
                entity.setUsercode(ConvertUtil.convertToString(user.get("code")));
                entity.setUsername(ConvertUtil.convertToString(user.get("name")));
                entity.setComponentid(ConvertUtil.convertToString(paraMap.get("componentid")));
                entity.setPosition(ConvertUtil.convertToString(paraMap.get("position")));
                entity.setRecordlevel("0");
                entity.setPriority("0");
                if(paraMap.containsKey("description")){
                    entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
                }
            entityList.add(entity);
            }
            mpmDataBoardCustomizeRecordService.insertBatch(entityList);
        }
        return ResponseResult.ok(paraMap);
    }
}

